package view;

import java.util.Vector;

import model.UserInfo;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.cloudgarden.resource.SWTResourceManager;

import controller.CommandsExcuter;
import controller.CommandsExcuter.CheckUserExcutor;
import controller.CommandsExcuter.DbSettingsExcutor;
import controller.CommandsExcuter.NewAccountExcutor;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class InstallWizard {
	private Display display;
	private Shell shell;
	private int index;
	private Button prev;
	private Button next;
	private Vector<Control> PAGES;
	private Composite wizardPanel;
	private StackLayout wizardLayout;
	private Vector<String> TITLES;
	private Text UserNameText;
	private Label UserNameLabel;
	private Text PasswordText;
	private Label Passwordlabel;
	private Label logoLabel;
	private Label emailErrorLabel;
	private Label matchErrorLabel;
	private Label ErrorLabel;
	private Label emailLabel;
	private Label repeatPassLabel;
	private Text RepPasswordText;
	private Text emailText;
	private Button cancelButton;
	private Button browseButton;
	private Text folderNameText;
	private Text locationText;
	private Text computerNameText;
	private Label FolderNameLabel;
	private Label locationLabel;
	private Label computerNameLabel;
	private boolean newClientFlag = true;
	private String curTitle;
	private Text dbPasswordText;
	private Text dbUserNameText;
	private Label dbPasswordLabel;
	private Label dbUserNameLabel;
	
	
	
	/**
	 * constructor of the class
	 */
	public InstallWizard(boolean newClientFlag) {

		this.newClientFlag=newClientFlag;
		display = Display.getDefault();
		shell = new Shell(display);
		
		//shell.setText("Add shared folder wizard");
				
		init();
		createGUI();
		fillPages();
		setSize(500, 400);
		//shell.setSize(450, 300);
		shell.open();
		while(!shell.isDisposed()) {
			if(!display.readAndDispatch()) {
				display.sleep();
			}
		}
		System.out.println("before display");
		display.dispose();
	}
	public static void main(String[] args) {
		@SuppressWarnings("unused")
		InstallWizard add= new InstallWizard(true);
		
	}
	/**
	 * fill all composites in the wizard
	 */
	private void fillPages() {
	
		//second composite definition
		Composite newAccountComposite = new Composite(shell, SWT.NULL);
		FormLayout thisLayout2 = new FormLayout();
		newAccountComposite.setLayout(thisLayout2);
		newAccountComposite.setSize(406, 259);
		newAccountComposite.layout();
		//email error label
		{
			emailErrorLabel = new Label(newAccountComposite, SWT.NONE);
			FormData emailErrorLabelLData = new FormData();
			emailErrorLabelLData.left =  new FormAttachment(0, 1000, 129);
			emailErrorLabelLData.top =  new FormAttachment(0, 1000, 11);
			emailErrorLabelLData.width = 125;
			emailErrorLabelLData.height = 14;
			emailErrorLabel.setLayoutData(emailErrorLabelLData);
			emailErrorLabel.setText("Email address isn't valid");
			emailErrorLabel.setForeground(SWTResourceManager.getColor(255, 0, 0));
			emailErrorLabel.setVisible(false);
		}
		{
			matchErrorLabel = new Label(newAccountComposite, SWT.NONE | SWT.CENTER);
			FormData matchErrorLabelLData = new FormData();
			matchErrorLabelLData.left =  new FormAttachment(0, 1000, 129);
			matchErrorLabelLData.top =  new FormAttachment(0, 1000, 2);
			matchErrorLabelLData.width = 282;
			matchErrorLabelLData.height = 31;
			matchErrorLabel.setLayoutData(matchErrorLabelLData);
			matchErrorLabel.setText("Password field and RepeatPassword field\n are not matching");
			matchErrorLabel.setForeground(SWTResourceManager.getColor(255, 0, 0));
			matchErrorLabel.setVisible(false);
		}
		{
			ErrorLabel = new Label(newAccountComposite, SWT.NONE);
			FormData ErrorLabelLData = new FormData();
			ErrorLabelLData.left =  new FormAttachment(0, 1000, 129);
			ErrorLabelLData.top =  new FormAttachment(0, 1000, 12);
			ErrorLabelLData.width = 129;
			ErrorLabelLData.height = 17;
			ErrorLabel.setLayoutData(ErrorLabelLData);
			ErrorLabel.setText("UserName already exist");
			ErrorLabel.setForeground(SWTResourceManager.getColor(255, 0, 0));
			ErrorLabel.setVisible(false);
		}
		{
			logoLabel = new Label(newAccountComposite, SWT.NONE);
			FormData logoLabelLData = new FormData();
			logoLabelLData.left =  new FormAttachment(0, 1000, 12);
			logoLabelLData.top =  new FormAttachment(0, 1000, 12);
			logoLabelLData.width = 136;
			logoLabelLData.height = 155;
			logoLabel.setLayoutData(logoLabelLData);
			logoLabel.setImage(SWTResourceManager.getImage("tour6.png"));//new Image(newAccountComposite.getDisplay(), "tour6.png"));
			logoLabel.setFont(SWTResourceManager.getFont("Tahoma", 16, 1, false, false));

		}
		{
			emailLabel = new Label(newAccountComposite, SWT.NONE);
			FormData emailLabelLData = new FormData();
			emailLabelLData.left =  new FormAttachment(0, 1000, 154);
			emailLabelLData.top =  new FormAttachment(0, 1000, 138);
			emailLabelLData.width = 28;
			emailLabelLData.height = 21;
			emailLabel.setLayoutData(emailLabelLData);
			emailLabel.setText("Email:");
		}
		{
			repeatPassLabel = new Label(newAccountComposite, SWT.NONE);
			FormData repeatPassLabelLData = new FormData();
			repeatPassLabelLData.left =  new FormAttachment(0, 1000, 154);
			repeatPassLabelLData.top =  new FormAttachment(0, 1000, 104);
			repeatPassLabelLData.width = 99;
			repeatPassLabelLData.height = 22;
			repeatPassLabel.setLayoutData(repeatPassLabelLData);
			repeatPassLabel.setText("Repeat Password:");
		}
		{
			FormData UserNameTextLData = new FormData();
			UserNameTextLData.left =  new FormAttachment(0, 1000, 259);
			UserNameTextLData.top =  new FormAttachment(0, 1000, 30);
			UserNameTextLData.width = 157;
			UserNameTextLData.height = 16;
			UserNameText = new Text(newAccountComposite, SWT.NONE | SWT.BORDER);
			UserNameText.setLayoutData(UserNameTextLData);
			UserNameText.setFocus();
			UserNameText.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					allFileldsFill(e);
				}
			});
			
		}
		{
			UserNameLabel = new Label(newAccountComposite, SWT.NONE);
			FormData UserNameLabelLData = new FormData();
			UserNameLabelLData.left =  new FormAttachment(0, 1000, 154);
			UserNameLabelLData.top =  new FormAttachment(0, 1000, 29);
			UserNameLabelLData.width = 66;
			UserNameLabelLData.height = 19;
			UserNameLabel.setLayoutData(UserNameLabelLData);
			UserNameLabel.setText("User name:");
		}
		{
			FormData PasswordTextLData = new FormData();
			PasswordTextLData.left =  new FormAttachment(0, 1000, 259);
			PasswordTextLData.top =  new FormAttachment(0, 1000, 68);
			PasswordTextLData.width = 156;
			PasswordTextLData.height = 19;
			PasswordText = new Text(newAccountComposite, SWT.BORDER | SWT.PASSWORD);
			PasswordText.setLayoutData(PasswordTextLData);
			PasswordText.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					allFileldsFill(e);
				}
			});
			
		}
		{
			Passwordlabel = new Label(newAccountComposite, SWT.NONE );
			FormData PasswordLData = new FormData();
			PasswordLData.left =  new FormAttachment(0, 1000, 154);
			PasswordLData.top =  new FormAttachment(0, 1000, 68);
			PasswordLData.width = 55;
			PasswordLData.height = 19;
			Passwordlabel.setLayoutData(PasswordLData);
			Passwordlabel.setText("Password:");
		}
		{
			FormData PasswordLData = new FormData();
			PasswordLData.left =  new FormAttachment(0, 1000, 259);
			PasswordLData.top =  new FormAttachment(0, 1000, 104);
			PasswordLData.width = 155;
			PasswordLData.height = 16;
			RepPasswordText = new Text(newAccountComposite, SWT.BORDER | SWT.PASSWORD);
			RepPasswordText.setLayoutData(PasswordLData);
			RepPasswordText.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					allFileldsFill(e);
				}
			});
			
		}
		{
			FormData emailTextLData = new FormData();
			emailTextLData.left =  new FormAttachment(0, 1000, 259);
			emailTextLData.top =  new FormAttachment(0, 1000, 138);
			emailTextLData.width = 155;
			emailTextLData.height = 16;
			emailText = new Text(newAccountComposite, SWT.BORDER);
			emailText.setLayoutData(emailTextLData);
			emailText.addKeyListener(new KeyAdapter() {
				public void keyPressed(KeyEvent evt) {
					emailTextKeyPressed(evt);
				}
				@Override
				public void keyReleased(KeyEvent e) {
					allFileldsFill(e);
				}
			});

		}
		if(newClientFlag){
			//add the composite to the wizard
			addPage(newAccountComposite, "Create new account");
			curTitle="Create new account";
			System.out.println("Create new account");
		}else
			curTitle="Computer settings";
		
		//third composite definition
		Composite computerConfigComposite = new Composite(shell, SWT.NULL);
		FormLayout thisLayout3 = new FormLayout();
		computerConfigComposite.setLayout(thisLayout3);
		FormData computerConfigCompositeLData = new FormData();
		computerConfigComposite.setLayoutData(computerConfigCompositeLData);
		computerConfigComposite.layout();
		/*//"open application now" check box
		{
			OpenAppButton = new Button(computerConfigComposite, SWT.CHECK | SWT.LEFT);
			FormData OpenAppButtonLData = new FormData();
			OpenAppButtonLData.left =  new FormAttachment(0, 1000, 207);
			OpenAppButtonLData.top =  new FormAttachment(0, 1000, 176);
			OpenAppButtonLData.width = 190;
			OpenAppButtonLData.height = 14;
			OpenAppButton.setLayoutData(OpenAppButtonLData);
			OpenAppButton.setText("launch CleverDropBox now ");
			OpenAppButton.setSelection(true);
		}*/
		
		//open folder dialog
		{
			browseButton = new Button(computerConfigComposite, SWT.PUSH | SWT.CENTER);
			FormData browseButtonLData = new FormData();
			browseButtonLData.left =  new FormAttachment(0, 1000, 372);
			browseButtonLData.top =  new FormAttachment(0, 1000, 88);
			browseButtonLData.width = 52;
			browseButtonLData.height = 26;
			browseButton.setLayoutData(browseButtonLData);
			browseButton.setText("Browse");
			browseButton.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent evt) {
					browseButtonWidgetSelected(evt);
				}
			});
		}
		//folder name text box
		{
			FormData folderNameTextLData = new FormData();
			folderNameTextLData.left =  new FormAttachment(0, 1000, 207);
			folderNameTextLData.top =  new FormAttachment(0, 1000, 141);
			folderNameTextLData.width = 141;
			folderNameTextLData.height = 17;
			folderNameText = new Text(computerConfigComposite, SWT.NONE| SWT.BORDER);
			folderNameText.setLayoutData(folderNameTextLData);
			if(!newClientFlag )
				folderNameText.setEnabled(false);
			folderNameText.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					allConfigFileldsFill(e);
				}
			});
		}
		//folder location text box
		{
			locationText = new Text(computerConfigComposite, SWT.NONE| SWT.BORDER);
			FormData locationTextLData = new FormData();
			locationTextLData.left =  new FormAttachment(0, 1000, 207);
			locationTextLData.top =  new FormAttachment(0, 1000, 88);
			locationTextLData.width = 140;
			locationTextLData.height = 19;
			locationText.setLayoutData(locationTextLData);
			locationText.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					allConfigFileldsFill(e);
				}
			});
		}
		//computer name text box
		{
			FormData computerNameTextLData = new FormData();
			computerNameTextLData.left =  new FormAttachment(0, 1000, 207);
			computerNameTextLData.top =  new FormAttachment(0, 1000, 31);
			computerNameTextLData.width = 143;
			computerNameTextLData.height = 18;
			computerNameText = new Text(computerConfigComposite, SWT.NONE | SWT.BORDER);
			computerNameText.setLayoutData(computerNameTextLData);
			computerNameText.setFocus();
			computerNameText.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					allConfigFileldsFill(e);
				}
			});
		}
		//folder name label
		{
			FolderNameLabel = new Label(computerConfigComposite, SWT.NONE);
			FormData FolderNameLabelLData = new FormData();
			FolderNameLabelLData.left =  new FormAttachment(0, 1000, 207);
			FolderNameLabelLData.top =  new FormAttachment(0, 1000, 119);
			FolderNameLabelLData.width = 73;
			FolderNameLabelLData.height = 16;
			FolderNameLabel.setLayoutData(FolderNameLabelLData);
			FolderNameLabel.setText("Folder name:");
			if(!newClientFlag)
				FolderNameLabel.setEnabled(false);
				
		}
		//location label
		{
			locationLabel = new Label(computerConfigComposite, SWT.NONE);
			FormData locationLabelLData = new FormData();
			locationLabelLData.left =  new FormAttachment(0, 1000, 207);
			locationLabelLData.top =  new FormAttachment(0, 1000, 67);
			locationLabelLData.width = 130;
			locationLabelLData.height = 15;
			locationLabel.setLayoutData(locationLabelLData);
			locationLabel.setText("DropBox folder location:");
		}
		//computer name label
		{
			computerNameLabel = new Label(computerConfigComposite, SWT.NONE);
			FormData computerNameLabelLData = new FormData();
			computerNameLabelLData.left =  new FormAttachment(0, 1000, 207);
			computerNameLabelLData.top =  new FormAttachment(0, 1000, 12);
			computerNameLabelLData.width = 93;
			computerNameLabelLData.height = 13;
			computerNameLabel.setLayoutData(computerNameLabelLData);
			computerNameLabel.setText("Computer name:");
		}
		//logo label
		{
			logoLabel = new Label(computerConfigComposite, SWT.NONE);
			FormData logoLabelLData = new FormData();
			logoLabelLData.left =  new FormAttachment(0, 1000, 12);
			logoLabelLData.top =  new FormAttachment(0, 1000, 12);
			logoLabelLData.width = 183;
			logoLabelLData.height = 178;
			logoLabel.setLayoutData(logoLabelLData);
			logoLabel.setImage(SWTResourceManager.getImage("tour6.png"));
			logoLabel.setFont(SWTResourceManager.getFont("Tahoma", 16, 1, false, false));

		}
		//add the composite to the wizard
		addPage(computerConfigComposite, "Computer settings");
		
		//third composite definition
		Composite dbSettingsComposite = new Composite(shell, SWT.NULL);
		FormLayout thisLayout4 = new FormLayout();
		dbSettingsComposite.setLayout(thisLayout4);
		//dbSettingsComposite.setSize(406, 259);
		dbSettingsComposite.layout();
		

		//db password text box
		{
			dbPasswordText = new Text(dbSettingsComposite, SWT.NONE| SWT.BORDER);
			FormData dbPasswordTextLData = new FormData();
			dbPasswordTextLData.left =  new FormAttachment(0, 1000, 207);
			dbPasswordTextLData.top =  new FormAttachment(0, 1000, 88);
			dbPasswordTextLData.width = 140;
			dbPasswordTextLData.height = 19;
			dbPasswordText.setLayoutData(dbPasswordTextLData);
			dbPasswordText.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					if(dbPasswordText.getText().compareTo("")!=0 && dbUserNameText.getText().compareTo("")!=0)
						next.setEnabled(true);
					else
						next.setEnabled(false);
				}
				public void keyPressed(KeyEvent e) {
					if(e.keyCode==13)
						next();
				}
			});
		}
		//db user name text box
		{
			FormData dbUserNameTextLData = new FormData();
			dbUserNameTextLData.left =  new FormAttachment(0, 1000, 207);
			dbUserNameTextLData.top =  new FormAttachment(0, 1000, 31);
			dbUserNameTextLData.width = 143;
			dbUserNameTextLData.height = 18;
			dbUserNameText = new Text(dbSettingsComposite, SWT.NONE | SWT.BORDER);
			dbUserNameText.setLayoutData(dbUserNameTextLData);
			dbUserNameText.setFocus();
			dbUserNameText.addKeyListener(new KeyAdapter() {
				@Override
				public void keyReleased(KeyEvent e) {
					if(dbPasswordText.getText().compareTo("")!=0 && dbUserNameText.getText().compareTo("")!=0)
						next.setEnabled(true);
					else
						next.setEnabled(false);
				}
			});
		}

		//db password label
		{
			dbPasswordLabel = new Label(dbSettingsComposite, SWT.NONE);
			FormData dbPasswordLabelLData = new FormData();
			dbPasswordLabelLData.left =  new FormAttachment(0, 1000, 207);
			dbPasswordLabelLData.top =  new FormAttachment(0, 1000, 67);
			dbPasswordLabelLData.width = 130;
			dbPasswordLabelLData.height = 15;
			dbPasswordLabel.setLayoutData(dbPasswordLabelLData);
			dbPasswordLabel.setText("Database password:");
		}
		//db user name label
		{
			dbUserNameLabel = new Label(dbSettingsComposite, SWT.NONE);
			FormData dbUserNameLabelLData = new FormData();
			dbUserNameLabelLData.left =  new FormAttachment(0, 1000, 207);
			dbUserNameLabelLData.top =  new FormAttachment(0, 1000, 12);
			dbUserNameLabelLData.width = 130;
			dbUserNameLabelLData.height = 19;
			dbUserNameLabel.setLayoutData(dbUserNameLabelLData);
			dbUserNameLabel.setText("Database user name:");
		}
		//logo label
		{
			logoLabel = new Label(dbSettingsComposite, SWT.NONE);
			FormData logoLabelLData = new FormData();
			logoLabelLData.left =  new FormAttachment(0, 1000, 12);
			logoLabelLData.top =  new FormAttachment(0, 1000, 12);
			logoLabelLData.width = 183;
			logoLabelLData.height = 178;
			logoLabel.setLayoutData(logoLabelLData);
			logoLabel.setImage(SWTResourceManager.getImage("tour6.png"));
			logoLabel.setFont(SWTResourceManager.getFont("Tahoma", 16, 1, false, false));

		}
		//add the composite to the wizard
		addPage(dbSettingsComposite, "database settings");
		
	}
	protected void allConfigFileldsFill(KeyEvent e) {
		System.out.println(folderNameText.getEnabled());
		if((folderNameText.getText().compareTo("")!=0 ||  !folderNameText.getEnabled()) && computerNameText.getText().compareTo("")!=0 && locationText.getText().compareTo("")!=0){
			next.setEnabled(true);
			System.out.println("all fileds are full");
		}
		else
			next.setEnabled(false);
		
		
	}
	protected void allFileldsFill(KeyEvent e) {
		if(RepPasswordText.getText().compareTo("")!=0 && PasswordText.getText().compareTo("")!=0 && UserNameText.getText().compareTo("")!=0
				&& emailText.getText().compareTo("")!=0){
			next.setEnabled(true);
			System.out.println("all fileds are full");
		}
		else
			next.setEnabled(false);
		
	}
	/*
	 * init' wizard components
	 */
	private void init() {
		index = 0; // index for current page
		PAGES = new Vector<Control>();
		TITLES = new Vector<String>();
	}
	/*
	 * create the wizard  
	 */
	private void createGUI() {
		shell.setLayout(new FormLayout());
		wizardPanel = new Composite(shell, SWT.NONE);

		
		wizardLayout = new StackLayout();
		FormData wizardPanelLData = new FormData();
		wizardPanelLData.width = 431;
		wizardPanelLData.height = 218;
		wizardPanel.setLayoutData(wizardPanelLData);
		wizardPanel.setLayout(wizardLayout);
		
		prev = new Button(shell, SWT.PUSH);
		next = new Button(shell, SWT.PUSH);
		cancelButton = new Button(shell, SWT.PUSH);
		cancelButton.setText("Cancel");
		FormData cancelButtonLData = new FormData();
		cancelButtonLData.width = 102;
		cancelButtonLData.height = 27;
		cancelButtonLData.left =  new FormAttachment(512, 1000, 0);
		cancelButtonLData.right =  new FormAttachment(747, 1000, 0);
		cancelButtonLData.top =  new FormAttachment(879, 1000, 0);
		cancelButtonLData.bottom =  new FormAttachment(982, 1000, 0);
		cancelButton.setLayoutData(cancelButtonLData);
		cancelButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				cancel();
			}
			});
		
		
		next.setText("Next >");
		next.addListener(SWT.Selection, new Listener() {
		public void handleEvent(Event e) {
			next.setEnabled(false);
			next();
		}
		});
		next.setEnabled(false);
		FormData nextLData = new FormData();
		nextLData.width = 115;
		nextLData.height = 27;
		nextLData.left =  new FormAttachment(233, 1000, 0);
		nextLData.right =  new FormAttachment(498, 1000, 0);
		nextLData.top =  new FormAttachment(879, 1000, 0);
		nextLData.bottom =  new FormAttachment(982, 1000, 0);
		next.setLayoutData(nextLData);

		prev.setText("< Previous");
		prev.addListener(SWT.Selection, new Listener() {
		public void handleEvent(Event e) {
		previous();
		}
		});
		prev.setEnabled(false);
		FormData prevLData = new FormData();
		prevLData.width = 83;
		prevLData.height = 27;
		prevLData.left =  new FormAttachment(28, 1000, 0);
		prevLData.right =  new FormAttachment(220, 1000, 0);
		prevLData.top =  new FormAttachment(879, 1000, 0);
		prevLData.bottom =  new FormAttachment(982, 1000, 0);
		prev.setLayoutData(prevLData);
	}
	/*
	 * "cancel" button selected
	 */
	private void cancel() {
		//shell.setVisible(false);
		startScreen inst = new startScreen(new Shell(), SWT.NULL);
		shell.dispose();
		inst.open();		
	}
	/*
	 * "next" button selected
	 */
	private void next() {
		//new Account page
		System.out.println(curTitle);
		//computer settings page
		if(curTitle.compareTo("Computer settings")==0){
			saveButt();
		}
		else if(curTitle.compareTo("Create new account")==0){
			RegisterSelected();
		}
		else if(curTitle.compareTo("database settings")==0){
			DbSettingsExcutor dbSettingsExcutor = CommandsExcuter.getCommand().getDbSettingsExcutor();
			dbSettingsExcutor.setDbUserName(dbUserNameText.getText());
			dbSettingsExcutor.setDbPassword(dbPasswordText.getText());
			dbSettingsExcutor.excute();
			NewAccountExcutor newAccount=CommandsExcuter.getCommand().getNewAccount();
			newAccount.excute();
			/*if(OpenAppButton.getSelection()){
				shell.dispose();
				AppScreen appScreen=new AppScreen(null, 0);
				appScreen.initGUI();
			}*/
			shell.dispose();
			return;
			
		}
		
		//not first page
		if(index-1 >= 0) {
			prev.setEnabled(true);
		}
		
		if(index+1 >= PAGES.size()) {
			
			next.setText("Finish");
		}
		/*//last page
		if(index == PAGES.size()-1 && "finish".equalsIgnoreCase(next.getText())) {
			//okButtonWidgetSelected();
			if(CommandsExcuter.getCommand().getAddFolder().getRetVal()){
				System.out.println("finished!");
				shell.setVisible(false);
				return;
			}else{
				
				MessageBox messageBox = new MessageBox(shell, SWT.ICON_ERROR);
				messageBox.setMessage("folder name is already in use or\nemail address is invalid\nplease try again...");
				messageBox.open();
			}
		}*/
	}
	
	/*
	 * "previous" button selected
	 */
	private void previous() {
		//not first page
		if(index > 0) {
			index -= 1;
			curTitle=TITLES.get(index);
			wizardLayout.topControl = PAGES.get(index);
			next.setEnabled(true);
			shell.setText("Wizard - " + TITLES.get(index));
			wizardPanel.layout();
		}
		//not last page
		if(index+1 < PAGES.size()) {
			next.setText("Next >");
		}
		//first page
		if(index-1 < 0) {
			prev.setEnabled(false);
		}
	}
	
	/*
	 * add new page to wizard
	 */
	private void addPage(Control c, String title) {
	
		c.setParent(wizardPanel);
		PAGES.addElement(c);
		TITLES.addElement(title);
		wizardLayout.topControl = PAGES.get(0);
		shell.setText("Wizard - " + TITLES.get(0));
	}
	/*
	 * set the shell size
	 */
	private void setSize(int width, int height) {
		shell.setSize(450, 300);
	}

	private void RegisterSelected() {
		matchErrorLabel.setVisible(false);
		emailErrorLabel.setVisible(false);
		ErrorLabel.setVisible(false);
		System.out.println(PasswordText.getText());
		System.out.println(RepPasswordText.getText());
		String str1=PasswordText.getText();
		String str2=RepPasswordText.getText();
		if(str1.compareTo(str2)!=0){
			System.out.println("enter");
			matchErrorLabel.setVisible(true);
			return;
		}
		if(!emailText.getText().contains("@")){
			emailErrorLabel.setVisible(true);
			return;
		}
		CheckUserExcutor checkUser=CommandsExcuter.getCommand().getCheckUser();
		checkUser.setUserName(UserNameText.getText());
		checkUser.setPassword(PasswordText.getText());
		checkUser.setEmail(emailText.getText());
		checkUser.excute();
		System.out.println(checkUser.getReturnVal());
		if(!CommandsExcuter.getCommand().getCheckUser().getReturnVal()){
			index += 1;
			curTitle= TITLES.get(index);
			wizardLayout.topControl = PAGES.get(index);
			shell.setText("Wizard - " + TITLES.get(index));
			wizardPanel.layout();
			allConfigFileldsFill(null);
		}
		else
			ErrorLabel.setVisible(true);

		
	}

	private void emailTextKeyPressed(KeyEvent evt) {
		if(evt.keyCode==13)
			RegisterSelected();
			
	}
	
	/*
	 * browse button selected
	 */
	private void browseButtonWidgetSelected(SelectionEvent evt) {
		DirectoryDialog dlg = new DirectoryDialog(shell);
		// Change the title bar text
        dlg.setText("DirectoryDialog");

        // Customizable message displayed in the dialog
        dlg.setMessage("Select a directory:");

        // Calling open() will open and run the dialog.
        // It will return the selected directory, or
        // null if user cancels
        String dir = dlg.open();
        if (dir != null) {
          // Set the text box to the new selection
          locationText.setText(dir);
          allConfigFileldsFill(null);
        }
	}
	
	/* 
	 * opartion when save button selected
	 */
	private void saveButt() {
		NewAccountExcutor newAccount=CommandsExcuter.getCommand().getNewAccount();
		newAccount.setComputerName(computerNameText.getText());
		newAccount.setPassword(UserInfo.getInstance().getPass());
		newAccount.setUserName(UserInfo.getInstance().getUserName());
		newAccount.setEmail(UserInfo.getInstance().getEmail());
		newAccount.setLocation(locationText.getText());
		if(!folderNameText.getEnabled()){
			newAccount.setFolderName(null);
		}
		else	
			newAccount.setFolderName(folderNameText.getText());
		
		index += 1;
		curTitle= TITLES.get(index);
		wizardLayout.topControl = PAGES.get(index);
		shell.setText("Wizard - " + TITLES.get(index));
		wizardPanel.layout();
		if(dbPasswordText.getText().compareTo("")!=0 && dbUserNameText.getText().compareTo("")!=0)
			next.setEnabled(true);
		else
			next.setEnabled(false);
		
		
	}

}
